/**
 * Copyright 2015 Software innovation and R & D center. All rights reserved.
 * File Name: DashBoardController.java
 * Encoding UTF-8
 * Version: 0.0.1
 * History:	2015年1月20日
 */
package com.sirdc.modules.sys.web;

import com.sirdc.modules.core.filter.Paging;
import com.sirdc.modules.core.web.BaseController;
import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.databean.SysUserDataBean;
import com.sirdc.modules.sys.entity.*;
import com.sirdc.modules.sys.filter.SysUserFilter;
import com.sirdc.modules.sys.security.SysPrincipal;
import com.sirdc.modules.sys.service.*;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.sys.util.UserUtils;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.insideDuCha.filter.InsideSubTaskFilter;
import net.huashitong.supervision.task.entity.TaskMain;
import net.huashitong.supervision.task.entity.TaskUnit;
import net.huashitong.supervision.task.entity.TaskUser;
import net.huashitong.supervision.task.filter.HastenMessageFilter;
import net.huashitong.supervision.task.filter.TaskMainFilter;
import net.huashitong.supervision.task.filter.TaskUnitFilter;
import net.huashitong.supervision.task.filter.TaskUserFilter;
import net.huashitong.supervision.task.service.*;
import net.huashitong.supervision.task.util.HolidayUtils;
import net.huashitong.synthetical.newSupervision.service.TaskMessageService;
import net.huashitong.synthetical.newSupervision.service.TaskNewService;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.text.ParseException;
import java.util.*;

/**
 * @version Revision: 0.0.1
 * @author: weihuang.peng
 * @Date: 2015年1月20日
 */
@Controller
@RequestMapping("/")
public class DashBoardController extends JsonBaseController {

    @Autowired
    private SysUserPermissionService userPermissionService;
    @Autowired
    private SysRolePermissionService rolePermissionService;
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private SysLoginService loginService;
    @Autowired
    private SysUserLoginService sysUserLoginService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private TaskNewService taskNewService;
    @Autowired
    private TaskMessageService taskMessageService;
    @Autowired
    private SysLoginService sysLoginService;
//    @Lazy
    @Autowired
    private TaskMainService taskMainService;
//    @Lazy
    @Autowired
    private TaskUnitService taskUnitService;
    @Autowired
    private CopeToService copeToService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private HastenMessageService hastenMessageService;

    @Autowired
    private SysLoginLogService sysLoginLogService;

    @Autowired
    private TaskNewListService taskNewListService;
    @Override
    protected String getView(String view) {
        return "/modules/" + view;
    }


    private int dwc = 0;//待完成条数
    private int ywc = 0;//已完成条数
    private int zb = 0;//在办条数
    private int cb = 0;//未读催办

    /**
     * @param model
     * @param request
     * @return
     */
    @RequestMapping
    public String index(Model model, HttpServletRequest request) {
        return "redirect:/manage";
    }

    @RequestMapping("/manage")
    @SuppressWarnings("unchecked")
    public String manage(Model model, HttpServletRequest request) throws ParseException {
        //返回当前用户
        Date date = new Date();
        String date1 = HolidayUtils.sdf1.format(date);
        String time = HolidayUtils.sdf2.format(date);
        SysLogin sysLogin = sysLoginService.getById(SysUserUtils.getUserId());
        SysDept dept = sysDeptService.getById(sysLogin.getDeptId());
        SysPrincipal principal = SysUserUtils.getSysPrincipal();

//        overdueScheduleService.overdueTask();//刷新任务是否逾期；
        //数字峰会
		/*if(ObjectUtils.isBlank(principal)){
			SysLogin loginUser = new SysLogin();
			loginUser.setSysId("admin");
			loginUser.setRoleId("01");
			loginUser.setDeptId("20150130100000000040");
			SysUser userInfo = new SysUser();
			userInfo.setSuperAdmin(true);
			userInfo.setName("admin");
			principal = new SysPrincipal(loginUser, userInfo);
		}*/

        SysUserLogin userLogin = new SysUserLogin();
        userLogin.setName(principal.getName());
        //  sysUserLoginService.save(userLogin);
        //数字峰会
        //更新用户的部门和角色
        userPermissionService.updateUserPrincipal(principal, request);
        //声明数据区
        List<SysUserPermission> userPermissions = null;
        List<SysMenu> menus = null;
        //缓存中的数据区
        Map<String, Object> cacheMap = principal.getCacheMap();
        if (cacheMap.size() == 0) {
            //为用户赋值部门和角色，以及可以选择的列表
            userPermissions = userPermissionService.queryByUserId(principal.getUserId());
            //获取菜单
            List<String> menuIds = rolePermissionService.queryMenuIdsByRoleIdDeptId(principal.getDeptId(), principal.getRoleId());
            menus = sysMenuService.queryMenuByIds(menuIds);
            //放入缓存
            cacheMap.put("menus", menus);
            cacheMap.put("userPermissions", userPermissions);
        } else {
            userPermissions = ((List<SysUserPermission>) cacheMap.get("userPermissions"));
            menus = (List<SysMenu>) cacheMap.get("menus");
        }
        /**
         * 将消息提醒与后台数据关联
         */
        //查询当前用户的未读信息
        //查询当前用户所负责的项目
        String userId = SysUserUtils.getUserId();
        /*============================================================================================*/

        int dcdb = 0;
        int dczb = 0;
        int dcwc = 0;
        if ("/01/".equals(dept.getDeptRelation())) {
            //督查单位
            List<TaskMain> listGjian = new ArrayList<>();
            TaskMainFilter taskMainFilter = new TaskMainFilter();
            taskMainFilter.setAuditorId(null);
            taskMainFilter.setStatus("9");
            taskMainFilter.setInspectionId(dept.getSysId());
            taskMainFilter.setCrucialUser("ZAIBAN");
            //查询攻坚系列
            listGjian = taskMainService.queryFilter(taskMainFilter);
            if ("02".equals(sysLogin.getRoleId())) {
                //如果是督查主要领导查询当前单位的所有待办事项
                taskMainFilter = new TaskMainFilter();
                taskMainFilter.setInspectionId(dept.getSysId());
                taskMainFilter.setStatus("3");
                taskMainFilter.setOther("DUCHA");
                List<TaskMain> taskMains3 = taskMainService.queryFilter(taskMainFilter);
                taskMainFilter.setStatus("4");
                taskMainFilter.setOther("");
                List<TaskMain> taskMains4 = taskMainService.queryFilter(taskMainFilter);
                dcwc = taskMains4.size();
                //在办
                List<TaskMain> unDoneFilter = taskMainService.getUnDoneFilter(taskMainFilter);
                dczb = unDoneFilter.size();
                //当前单位领导的待审批事项
                taskMainFilter.setAuditorId(sysLogin.getSysId());
                taskMainFilter.setStatus("0");
                List<TaskMain> taskMains1 = taskMainService.queryFilter(taskMainFilter);
                //查询攻坚派发给单位的未立项的事项
                if ("/01/".equals(dept.getDeptRelation()) && !dept.getDeptName().startsWith("攻坚")) {
                    //不是攻坚办
                    taskMainFilter.setAuditorId(null);
                    taskMainFilter.setStatus("9");
                    taskMainFilter.setInspectionId(dept.getSysId());
                    taskMainFilter.setCrucialUser("ZAIBAN");
                    //查询攻坚系列
                    listGjian = taskMainService.queryFilter(taskMainFilter);
                }
                dcdb = taskMains3.size() + taskMains1.size() + listGjian.size();
            } else {
                //如果不是只查询自己的待办事项
                 taskMainFilter = new TaskMainFilter();
                taskMainFilter.setCreateUser(sysLogin.getSysId());
                taskMainFilter.setStatus("3");
                taskMainFilter.setOther("DUCHA");
                List<TaskMain> taskMains3 = taskMainService.queryFilter(taskMainFilter);
                taskMainFilter.setStatus("4");
                taskMainFilter.setOther("");

                List<TaskMain> taskMains4 = taskMainService.queryFilter(taskMainFilter);
                //在办
                List<TaskMain> unDoneFilter = taskMainService.getUnDoneFilter(taskMainFilter);
                dczb = unDoneFilter.size();
                dcwc = taskMains4.size();
                //当前单位其他人的待审批事项
                taskMainFilter.setStatus(null);
                taskMainFilter.setCreateUser(null);
                taskMainFilter.setAuditorId(sysLogin.getSysId());
                taskMainFilter.setStatus("0");
                List<TaskMain> taskMains1 = taskMainService.queryFilter(taskMainFilter);
                dcdb = taskMains3.size() + taskMains1.size()+listGjian.size();
            }
            if ("/01/".equals(dept.getDeptRelation()) && dept.getDeptName().startsWith("攻坚")) {
                //攻坚办
               taskMainFilter = new TaskMainFilter();
                taskMainFilter.setAuditorId(null);
                taskMainFilter.setStatus("4");
                if ("02".equals(sysLogin.getRoleId())) {
                    // 所有的攻坚 主任务 都是领导看的
                    taskMainFilter.setCrucialUser("ALL");
                } else {
                    taskMainFilter.setCrucialUser(userId);
                }
                //查询攻坚系列在办
                List<TaskMain> listZBGjian = taskMainService.getUnDoneFilter(taskMainFilter);
                //攻坚已完成
                List<TaskMain> listWCGjian = taskMainService.queryFilter(taskMainFilter);
                //攻坚待办
                taskMainFilter.setStatus(null);
                taskMainFilter.setCrucialState("0");
                List<TaskMain> listDBGjian = taskMainService.queryFilter(taskMainFilter);
                dwc=dcdb+listDBGjian.size();
                zb = dczb + listZBGjian.size();
                ywc=dcwc+listWCGjian.size();
            } else {
                zb = dczb;
                ywc=dcwc;
                dwc=dcdb;
            }
        } else {
            //如果不是督查组
            if ("02".equals(sysLogin.getRoleId())) {
                //如果是承办单位领导
                TaskUnitFilter taskUnitFilter = new TaskUnitFilter();
                taskUnitFilter.setDeptId(dept.getSysId());

                List<TaskUnit> unDoneUnit = taskUnitService.getUnDoneUnit(taskUnitFilter);
                taskUnitFilter.setType("4");
                List<TaskUnit> doneUnit = taskUnitService.queryFilter(taskUnitFilter);
                //单位重大事项
                TaskMainFilter filter = new TaskMainFilter();
                filter.setOverdueDept(dept.getSysId());
                //待办单位重大
                List<TaskMain> overdueDBDeptMain = taskMainService.queryDBOverdueDept(filter);
                //单位在办督办任务
                List<TaskMain> overdueZBDeptMain = taskMainService.queryZBOverdueDept(filter);
                //单位已完成的重大事项
                List<TaskMain> overdueWCDeptMain = taskMainService.queryWCOverdueDept(filter);
                ywc = doneUnit.size()+overdueWCDeptMain.size();
                dwc = unDoneUnit.size()+overdueDBDeptMain.size();
                //在办
                zb = taskUnitService.getNowUnit(taskUnitFilter).size()+overdueZBDeptMain.size();

            } else {
                //只查看自己的任务
                TaskUnitFilter filter = new TaskUnitFilter();
                filter.setUserId(SysUserUtils.getUserId());
                //待办
                List<TaskUnit> UnDoneUser = taskUnitService.getUnDoneUser(filter);
                List<TaskUnit> DoneUser = taskUnitService.getDoneUser(filter);
                //在办
                zb = taskUnitService.getNowUser(filter).size();
                dwc = UnDoneUser.size();
                ywc = DoneUser.size();
            }
            //未读催办
            HastenMessageFilter hastenMessageFilter = new HastenMessageFilter();
            hastenMessageFilter.setUserId(userId);
            hastenMessageFilter.setStatus("0");
            int size = hastenMessageService.queryFilter(hastenMessageFilter).size();
            cb = size;
        }
        //查询抄送当前用户的待完成个数
        TaskMainFilter filter = new TaskMainFilter();
        filter.setCopeTo(userId);
        List<TaskMain> unDoneCope = copeToService.queryUnDone(filter);
        List<TaskMain> doneCope = copeToService.queryDone(filter);
        dwc += unDoneCope.size();
        ywc += doneCope.size();
//        model.addAttribute("messageCount", messageCount);
        model.addAttribute("userPermissions", userPermissions);
        model.addAttribute("menus", menus);
        model.addAttribute("dept", dept);
        model.addAttribute("dwc", dwc);
        model.addAttribute("ywc", ywc);
        model.addAttribute("zb", zb);
        model.addAttribute("cb", cb);
        //判断用户必填信息是否填写
        SysUser user = sysUserService.getById(userId);
        SysLogin login = sysLoginService.getById(userId);
        String encryPass = sysUserService.getEncryPass("123456", login.getSaltKey());
        if (StringUtils.isBlank(user.getMobile())||encryPass.equals(login.getPassword())){
            //如果用户电话信息为空或者密码为123456，返回修改必填信息页面
            model.addAttribute("user", user);
            model.addAttribute("userLogin", login);
            if (encryPass.equals(login.getPassword())) {
                //密码为123456
                model.addAttribute("login", true);
            }else{
                model.addAttribute("login",false);
            }
            return getView("update");
        }

        /**
         * 取出当前登录ip,保存登录日志
         */

            String ip = request.getHeader("x-forwarded-for");
            if (StringUtils.isBlank(ip)) {
                ip = request.getRemoteAddr();
            }
            SysLoginLog sysLoginLog = new SysLoginLog();
            sysLoginLog.setIp(ip);
            sysLoginLog.setUserName(login.getUsername());
            sysLoginLog.setLoginDate(date1);
            sysLoginLog.setLoginTime(time);
            sysLoginLog.setDeptId(login.getDeptId());
            sysLoginLog.setDeptName(dept.getDeptName());
            sysLoginLog.setRoleId(login.getRoleId());
            sysLoginLog.setUserId(login.getSysId());
            sysLoginLog.setDemo(dwc + "");//待完成个数
            sysLoginLog.setState("1");//web端；
            sysLoginLogService.save(sysLoginLog, userId);

            /*
              未反馈和已反馈的统计
             */
        int taskCount = getTaskCount("0", "0");
        int taskCount1 = getTaskCount("1", "1");
        model.addAttribute("unsignCount",taskCount);
        model.addAttribute("unbackCount",taskCount1);

        return getView("frame");
    }

    //未签收个数
    private int getTaskCount(String type,String insideTaskState){
        /**
         * 未签收普通事项
         */
        TaskUnitFilter filter1=new TaskUnitFilter();
        Paging page= new Paging();
        filter1.setPaging(page);
        filter1.getPaging().setPageSize(10);
        filter1.getPaging().setCurrentPage(1);
        filter1.getPaging().setSortVal("asc");
        filter1.setType(type);
//        filter.setType("0");

        /**
         * 未签收重大事项
         */
        TaskUnitFilter filter2=new TaskUnitFilter();
        Paging page2= new Paging();
        filter2.setPaging(page2);
        filter2.getPaging().setPageSize(10);
        filter2.getPaging().setCurrentPage(1);
        filter2.getPaging().setSortVal("asc");
        filter2.setImp(1);
        filter2.setType(type);


        String roleId = SysUserUtils.getRoleId();
        String loginId = SysUserUtils.getUserId();
        String deptId = SysUserUtils.getDeptId();

        if(StringUtils.isBlank(loginId))
        {
            return 0;

        }
        if("04".equals(roleId)){
            filter1.setUserId(loginId);
            filter2.setUserId(loginId);

        }else if("02".equals(roleId)) {
            filter1.setUserId(loginId);
            filter1.setCreateUser(loginId);
            filter1.setDeptId(deptId);

            filter2.setUserId(loginId);
            filter2.setCreateUser(loginId);
            filter2.setDeptId(deptId);
        }else {
            filter1.setDeptId(null);
            filter1.setUserId(loginId);

            filter2.setDeptId(null);
            filter2.setUserId(loginId);
        }
        List<TaskUnit> normalTaskList = taskNewListService.getNormalTaskList(filter1);


        List<TaskUnit> zhongdaList = taskNewListService.getNormalTaskList(filter2);

        /**
         * 未签收内部督查事项
         *
         */
        InsideSubTaskFilter insideFilter = new InsideSubTaskFilter();
        Paging page3= new Paging();
        insideFilter.setPaging(page3);
        insideFilter.getPaging().setPageSize(10);
        insideFilter.getPaging().setCurrentPage(1);
        insideFilter.getPaging().setSortVal("asc");
        insideFilter.setTaskState(insideTaskState);
//        insideFilter.setTaskState(0+"");
        List<InsideSubTaskFilter> insideTask = taskNewListService.getInsideTask(insideFilter);

        int records1 = filter1.getPaging().getRecords();
        int records2 = filter2.getPaging().getRecords();
        int records3 = insideFilter.getPaging().getRecords();

        int total= records1+records2+records3;


        return total;
    }



    @RequestMapping("/mainContainer")
    public String mainContainer(HttpServletRequest request, HttpServletResponse response, Model model) {
        //返回当前用户
        SysLogin sysLogin = sysLoginService.getById(SysUserUtils.getUserId());
        SysUser user = sysUserService.getById(SysUserUtils.getUserId());
        SysDept dept = sysDeptService.getById(sysLogin.getDeptId());
        List<Map> dcMapList = new ArrayList<>();
        List<Map> xiangzhendwMapList = new ArrayList<>();
        List<Map> xianzhidwMapList = new ArrayList<>();
        //报表
        if ("/01/".equals(dept.getDeptRelation())) {
            //如果当前用户是督查组
            if ("02".equals(sysLogin.getRoleId())) {
                //如果是单位领导
            } else if ("03".equals(sysLogin.getRoleId())) {
                //如果是分管，查看当前单位的所有分管的任务
                SysUserFilter sysUserFilter = new SysUserFilter();
                sysUserFilter.setRoleId("03");
                sysUserFilter.setDeptId(dept.getSysId());
                List<SysLogin> loginList = sysLoginService.queryUserLoginByRoleDept(sysUserFilter);
                for (SysLogin login : loginList) {
                   /* //查看分管的任务数
                    TaskUserFilter taskUserFilter = new TaskUserFilter();
                    taskUserFilter.setUnitUserId(login.getSysId());
                    List<TaskUser> allTask = taskUserService.queryFilter(taskUserFilter);
                    taskUserFilter.setStatus("3");
                    //重办任务
                    List<TaskUser> cbTask = taskUserService.queryFilter(taskUserFilter);
                    int cb = cbTask.size();
                    int all = allTask.size();
                    Map<String,String>map=new HashMap<>();
                    SysUser fgUser = sysUserService.getById(login.getSysId());
                    map.put("name",fgUser.getName());
                    map.put("all", all+"");
                    map.put("cb",cb+"");
                    dcMapList.add(map);*/
                }
            } else if ("04".equals(sysLogin.getRoleId())) {
            }
        } else if ("/02/201/".equals(dept.getDeptRelation())) {
            //如果是乡镇单位取出说有乡镇单位
            List<SysDept> list = deptService.queryDeptList("/02/201/");
            for (SysDept sysDept : list) {
                //根据当前单位查询该单位的任务
                Map<String, String> map = new HashMap<>();
                TaskUnitFilter taskUnitFilter = new TaskUnitFilter();
                taskUnitFilter.setDeptId(sysDept.getSysId());
                //任务数
                int allSize = taskUnitService.queryFilter(taskUnitFilter).size();
                taskUnitFilter.setType("3");
                //重办数
                int cbSize = taskUnitService.queryFilter(taskUnitFilter).size();
                //逾期数
                taskUnitFilter.setType(null);
                taskUnitFilter.setStatus("10");
                int yqSize = taskUnitService.queryFilter(taskUnitFilter).size();
                map.put("allSize", allSize + "");
                map.put("cbSize", cbSize + "");
                map.put("yqSize", yqSize + "");
                map.put("deptName", sysDept.getDeptName());
                xiangzhendwMapList.add(map);
            }
        } else if ("/02/202/".equals(dept.getDeptRelation())) {
            //如果是县直单位取出说有县直单位
            List<SysDept> list = deptService.queryDeptList("/02/202/");
            for (SysDept sysDept : list) {
                //根据当前单位查询该单位的任务
                Map<String, String> map = new HashMap<>();
                TaskUnitFilter taskUnitFilter = new TaskUnitFilter();
                taskUnitFilter.setDeptId(sysDept.getSysId());
                //任务数
                int allSize = taskUnitService.queryFilter(taskUnitFilter).size();
                taskUnitFilter.setType("3");
                //重办数
                int cbSize = taskUnitService.queryFilter(taskUnitFilter).size();
                //逾期数
                taskUnitFilter.setType(null);
                taskUnitFilter.setStatus("10");
                int yqSize = taskUnitService.queryFilter(taskUnitFilter).size();
                map.put("allSize", allSize + "");
                map.put("cbSize", cbSize + "");
                map.put("yqSize", yqSize + "");
                map.put("deptName", sysDept.getDeptName());
                xianzhidwMapList.add(map);
            }
        }
        //乡镇单位表格
        model.addAttribute("xiangzhendwMapList", xiangzhendwMapList);
        //县直单位
        model.addAttribute("xianzhidwMapList", xianzhidwMapList);
        model.addAttribute("dept", dept);
        model.addAttribute("dwc", dwc);
        model.addAttribute("ywc", ywc);
        model.addAttribute("zb", zb);
        model.addAttribute("cb", cb);

        return getView("home");
    }

    /**
     * 修改用户的部门和角色，并刷新缓存的数据
     *
     * @author: weihuang.peng
     */
    @RequestMapping("/changeDeptRole")
    public String changeDeptRole(@Valid @Length(min = 20, max = 20) @RequestParam(required = false) String deptId, @Valid @Length(min = 2, max = 4) @RequestParam(required = false) String roleId) {
        SysPrincipal principal = SysUserUtils.getSysPrincipal();

        //判断用户是否拥有此权限,用户自身 + 用户权限信息
        if (userPermissionService.isExistUserPermission(principal.getUserId(), roleId, deptId)
                || loginService.isExistUserPermission(principal.getUserId(), roleId, deptId)) {
            //更新用户的部门和角色
            userPermissionService.changeUserRoleDept(principal, deptId, roleId);
            //清空缓存信息
            UserUtils.clearCacheMap();
        }
        return "redirect:/manage";
    }

    @ResponseBody
    @RequestMapping(value = "/menusNum")
    public Message menusNum(SysUserDataBean dataBean){
        Map<String ,Object> map=new HashMap<>();
        String userId="";
        if (StringUtils.isNotBlank(dataBean.getUserId())){
            userId=dataBean.getUserId();
        }else{
            userId=SysUserUtils.getUserId();
        }

        if (StringUtils.isBlank(userId)){
            return coverMessage("500");
        }
        SysLogin sysLogin = sysLoginService.getById(userId);
        SysDept dept = deptService.getById(sysLogin.getDeptId());
        int dcdb = 0;
        int dczb = 0;
        int dcwc = 0;
        if ("/01/".equals(dept.getDeptRelation())) {
            //督查单位
            if ("02".equals(sysLogin.getRoleId())) {
                //如果是督查主要领导查询当前单位的所有待办事项
                TaskMainFilter taskMainFilter = new TaskMainFilter();
                taskMainFilter.setInspectionId(dept.getSysId());
                taskMainFilter.setStatus("3");
                taskMainFilter.setOther("DUCHA");
                List<TaskMain> taskMains3 = taskMainService.queryFilter(taskMainFilter);
                taskMainFilter.setStatus("4");
                taskMainFilter.setOther("");
                List<TaskMain> taskMains4 = taskMainService.queryFilter(taskMainFilter);
                dcwc = taskMains4.size();
                //在办
                List<TaskMain> unDoneFilter = taskMainService.getUnDoneFilter(taskMainFilter);
                dczb = unDoneFilter.size();
                //当前单位领导的待审批事项
                taskMainFilter.setAuditorId(sysLogin.getSysId());
                taskMainFilter.setStatus("0");
                List<TaskMain> taskMains1 = taskMainService.queryFilter(taskMainFilter);
                //查询攻坚派发给单位的未立项的事项
                List<TaskMain> listGjian = new ArrayList<>();
                if ("/01/".equals(dept.getDeptRelation()) && !dept.getDeptName().startsWith("攻坚")) {
                    //不是攻坚办
                    taskMainFilter.setAuditorId(null);
                    taskMainFilter.setStatus("9");
                    taskMainFilter.setInspectionId(dept.getSysId());
                    taskMainFilter.setCrucialUser("ZAIBAN");
                    //查询攻坚系列
                    listGjian = taskMainService.queryFilter(taskMainFilter);
                }
                dcdb = taskMains3.size() + taskMains1.size() + listGjian.size();
            } else {
                //如果不是只查询自己的待办事项
                TaskMainFilter taskMainFilter = new TaskMainFilter();
                taskMainFilter.setCreateUser(sysLogin.getSysId());
                taskMainFilter.setStatus("3");
                taskMainFilter.setOther("DUCHA");
                List<TaskMain> taskMains3 = taskMainService.queryFilter(taskMainFilter);
                taskMainFilter.setStatus("4");
                taskMainFilter.setOther("");

                List<TaskMain> taskMains4 = taskMainService.queryFilter(taskMainFilter);
                //在办
                List<TaskMain> unDoneFilter = taskMainService.getUnDoneFilter(taskMainFilter);
                dczb = unDoneFilter.size();
                dcwc = taskMains4.size();
                //当前单位其他人的待审批事项
                taskMainFilter.setStatus(null);
                taskMainFilter.setCreateUser(null);
                taskMainFilter.setAuditorId(sysLogin.getSysId());
                taskMainFilter.setStatus("0");
                List<TaskMain> taskMains1 = taskMainService.queryFilter(taskMainFilter);
                dcdb = taskMains3.size() + taskMains1.size();
            }
            if ("/01/".equals(dept.getDeptRelation()) && dept.getDeptName().startsWith("攻坚")) {
                //攻坚办
                TaskMainFilter taskMainFilter = new TaskMainFilter();
                taskMainFilter.setAuditorId(null);
                taskMainFilter.setStatus("4");
                if ("02".equals(sysLogin.getRoleId())) {
                    // 所有的攻坚 主任务 都是领导看的
                    taskMainFilter.setCrucialUser("ALL");
                } else {
                    taskMainFilter.setCrucialUser(userId);
                }
                //查询攻坚系列在办
                List<TaskMain> listZBGjian = taskMainService.getUnDoneFilter(taskMainFilter);
                //攻坚已完成
                List<TaskMain> listWCGjian = taskMainService.queryFilter(taskMainFilter);
                //攻坚待办
                taskMainFilter.setStatus(null);
                taskMainFilter.setCrucialState("0");
                List<TaskMain> listDBGjian = taskMainService.queryFilter(taskMainFilter);
                dwc=dcdb+listDBGjian.size();
                zb = dczb + listZBGjian.size();
                ywc=dcwc+listWCGjian.size();
            } else {
                zb = dczb;
                ywc=dcwc;
                dwc=dcdb;
            }
        } else {
            //如果不是督查组
            if ("02".equals(sysLogin.getRoleId())) {
                //如果是承办单位领导
                TaskUnitFilter taskUnitFilter = new TaskUnitFilter();
                taskUnitFilter.setDeptId(dept.getSysId());

                List<TaskUnit> unDoneUnit = taskUnitService.getUnDoneUnit(taskUnitFilter);
                taskUnitFilter.setType("4");
                List<TaskUnit> doneUnit = taskUnitService.queryFilter(taskUnitFilter);
                //单位重大事项
                TaskMainFilter filter = new TaskMainFilter();
                filter.setOverdueDept(dept.getSysId());
                //待办单位重大
                List<TaskMain> overdueDBDeptMain = taskMainService.queryDBOverdueDept(filter);
                //单位在办督办任务
                List<TaskMain> overdueZBDeptMain = taskMainService.queryZBOverdueDept(filter);
                //单位已完成的重大事项
                List<TaskMain> overdueWCDeptMain = taskMainService.queryWCOverdueDept(filter);
                ywc = doneUnit.size()+overdueWCDeptMain.size();
                dwc = unDoneUnit.size()+overdueDBDeptMain.size();
                //在办
                zb = taskUnitService.getNowUnit(taskUnitFilter).size()+overdueZBDeptMain.size();

            } else {
                //只查看自己的任务
                TaskUnitFilter filter = new TaskUnitFilter();
                filter.setUserId(SysUserUtils.getUserId());
                //待办
                List<TaskUnit> UnDoneUser = taskUnitService.getUnDoneUser(filter);
                List<TaskUnit> DoneUser = taskUnitService.getDoneUser(filter);
                //在办
                zb = taskUnitService.getNowUser(filter).size();
                dwc = UnDoneUser.size();
                ywc = DoneUser.size();
            }
            //未读催办
            HastenMessageFilter hastenMessageFilter = new HastenMessageFilter();
            hastenMessageFilter.setUserId(userId);
            hastenMessageFilter.setStatus("0");
            int size = hastenMessageService.queryFilter(hastenMessageFilter).size();
            cb = size;
        }
        //查询抄送当前用户的待完成个数
        TaskMainFilter filter = new TaskMainFilter();
        filter.setCopeTo(userId);
        List<TaskMain> unDoneCope = copeToService.queryUnDone(filter);
        List<TaskMain> doneCope = copeToService.queryDone(filter);
        dwc += unDoneCope.size();
        ywc += doneCope.size();
//        model.addAttribute("messageCount", messageCount);
        map.put("dept", dept);
        map.put("dwc", dwc);
        map.put("ywc", ywc);
        map.put("zb", zb);
        map.put("cb", cb);
        return coverMessage("200","success",map);
    }
}